package com.lfs.notify.service.impl;


import com.lfs.base.util.CollectionUtils;
import com.lfs.common.constant.CommonConstants;
import com.lfs.interfaces.dao.OrderChargeDao;
import com.lfs.interfaces.model.dto.OrderCharge;
import com.lfs.interfaces.model.dto.OrderDto;
import com.lfs.interfaces.model.dto.OrderNotifyDto;
import com.lfs.interfaces.order.service.OrderBaseService;
import com.lfs.interfaces.product.service.ProductService;
import com.lfs.notify.dao.NotifyDao;
import com.lfs.notify.service.NotifyService;
import com.lfs.notify.service.zfb.service.impl.ZFBNotifyServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NotifyServiceImpl implements NotifyService {

	private static final Logger logger = LoggerFactory.getLogger(ZFBNotifyServiceImpl.class);

	@Resource
	private ProductService productService;

	@Resource
	private OrderChargeDao orderChargeDao;

	@Resource
	private OrderBaseService orderBaseService;

	@Resource
	private NotifyDao notifyDao;

	@Override
	public int updateAccountOrder(String orderNo, String state, String upOrderNo) {
		return notifyDao.updateAccountOrder(orderNo, state, upOrderNo);
	}

	@Override
	public String notifyBank(String orderNo, String status, String upOrderNo) {
		String result = "success";
		try {
			OrderCharge order = new OrderCharge();
			order.setOrderNo(orderNo);
			// order.setUpOrderNo(upOrderNo);
			List<OrderCharge> orders = orderChargeDao.getValidOrderForConfirmByBuscodeAndOrderNo(order);
			if (CollectionUtils.isNotEmpty(orders)) {
				OrderCharge orderInfo = orders.get(0);
				OrderNotifyDto orderNotify = new OrderNotifyDto();
				orderNotify.setAgtPhone(orderInfo.getAgtPhone());
				orderNotify.setReqStreamId(orderInfo.getDownOrderNo());
				orderNotify.setOrderNo(orderNo);
				orderNotify.setPrice(orderInfo.getPrice());
				orderNotify.setChargeType(orderInfo.getChargeType());

				orderInfo.setOutMoney(orderInfo.getOutMoney());
				orderInfo.setProfit(orderInfo.getProfit());
				OrderDto orderDto = new OrderDto();
				orderDto.setId(orderInfo.getId());
				orderDto.setOrderNo(orderInfo.getOrderNo());
				orderDto.setOutMoney(orderInfo.getOutMoney());
				orderDto.setProfit(orderInfo.getProfit());
				orderDto.setAgtNo(orderInfo.getAgtNo());
				orderDto.setChargeAddr(orderInfo.getChargeAddr());
				orderDto.setChargeMoney(orderInfo.getChargeMoney());
				orderDto.setUpOrderNo(upOrderNo == null ? "" : upOrderNo);
				if ("0".equals(status)) {
					// 回写回调表
					if (orderNotify.getReqStreamId() != null && orderNotify.getReqStreamId() != "") {
						orderNotify.setState(0);
						result = orderBaseService.sendNotify(orderNotify);
						if(result.equals(CommonConstants.NOTIFY_DOWN_SUCCESS)){
							// 成功
							orderDto.setState(0);
							orderDto.setRemark("充值成功|回调");
						}else{
							// 成功
							orderDto.setState(5);
							orderDto.setRemark("充值成功|回调下游失败");
						}
						orderBaseService.updateOrderAfterNotify(orderDto);
					}
				} else if ("1".equals(status)) {
					// 回写回调表
					if (orderNotify.getReqStreamId() != null && orderNotify.getReqStreamId() != "") {
						orderNotify.setState(1);
						result =orderBaseService.sendNotify(orderNotify);
						if(result.equals(CommonConstants.NOTIFY_DOWN_SUCCESS)){
							// 失败，修改订单状态
							orderDto.setState(1);
							orderDto.setRemark("充值失败|回调");
						}else{
							// 失败，修改订单状态
							orderDto.setState(6);
							orderDto.setRemark("充值失败|回调下游失败");
						}
						orderBaseService.updateOrderAfterNotify(orderDto);
					}
				}
			} else {
				return result;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			result = "Exception";
		}
		return result;
	}

}
